package com.magupe.im.web.client.config;

import com.magupe.im.web.client.common.freemarker.FreeMarkerVariablesProperties;
import freemarker.template.TemplateModelException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xing
 */
@Configuration
@EnableConfigurationProperties(FreeMarkerVariablesProperties.class)
public class FreemarkerConfig {

    final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private FreeMarkerVariablesProperties variablesProperties;
    @Resource
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @PostConstruct
    public void setSharedVariable() throws TemplateModelException {
        freemarker.template.Configuration cfg = freeMarkerConfigurer.getConfiguration();
        Map<String, Object> variables = new HashMap<>(32);
        variables.putAll(variablesProperties.getDirectiveBeanMap());
        variables.putAll(variablesProperties.getCons());
        cfg.setSharedVariables(variables);
    }
}
